<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataTakenLandService;

class TakenLandController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/taken-land/get-list 01、大数据土地搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataTakenLand
     *
     * @apiDescription 01、大数据土地搜索
     *
     * @apiParam {String} keyword                           搜索词
     * @apiParam {String} province                          省份
     * @apiParam {String} city                              城市
     * @apiParam {String} company_type                      企业类型
     * @apiParam {String} land_type                         土地类型（如果是多个，使用英文逗号拼接  eg: A,B）
     * @apiParam {String} area_lower/area_upper             拿地面积
     * @apiParam {String} unit_price_lower/unit_price_upper 拿地单价
     * @apiParam {String} land_price_lower/land_price_upper 成交价格
     * @apiParam {String} is_price_gt_avg                   高端项目标签
     * @apiParam {String} page                              页码
     * @apiParam {String} pageSize                          分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionGetList()
    {
        try {
            $keyword = $this->getParam('keyword');
            $province = $this->getParam('province');
            $city = $this->getParam('city');
            $companyType = $this->getParam('company_type');
            $landType = trim($this->getParam('land_type')) ? explode(',', trim($this->getParam('land_type'))) : [];
            $areaLower = $this->getParam('area_lower');
            $areaUpper = $this->getParam('area_upper');
            $unitPriceLower = $this->getParam('unit_price_lower');
            $unitPriceUpper = $this->getParam('unit_price_upper');
            $landPriceLower = $this->getParam('land_price_lower');
            $landPriceUpper = $this->getParam('land_price_upper');
            $isPriceGtAvg = $this->getParam('is_price_gt_avg');
            $areaStr = $this->getParam('area');
            $companyArea = trim($this->getParam('company_area')) ? explode(',', trim($this->getParam('company_area'))) : [];
            $organizingCode = trim($this->getParam('organizing_code')) ? explode(',', trim($this->getParam('organizing_code'))) : [];
            $createdTime = $this->getParam('created_time');
            $takeLandDateStart = $this->getParam('take_land_date_start');
            $takeLandDateEnd = $this->getParam('take_land_date_end');
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            $tags = [];
            $tags['developer_top_100_label'] = $this->getParam('developer_top_100_label');//地产百强
            $tags['new_decorate_expand_label'] = $this->getParam('new_decorate_expand_label');//新精装客群
            $tags['new_land_usage_expand_label'] = $this->getParam('new_land_usage_expand_label');//新住宅客群
            $tags['new_area_expand_label'] = $this->getParam('new_area_expand_label');//新区域拓展
            $tags['price_gt_avg_label'] = $this->getParam('price_gt_avg_label'); //高端项目客群
            $tags['urban_investment_label'] = $this->getParam('urban_investment_label'); //城投企业

            if (!empty($province)) {
                $province = explode(",", $province);
            } else {
                $province = [];
            }
            $city = !empty($city) ? explode(",", $city) : [];
            $area = array_filter(explode(",", $areaStr));
            $isPriceGtAvg = array_filter(explode(",", $isPriceGtAvg));
            $keyword = array_filter(explode(",", $keyword));
            $companyTypeArr = !empty($companyType) ? explode(",", $companyType) : [];

            $bigDataDeveloperService = new BigDataTakenLandService();
            $rs = $bigDataDeveloperService->getDataList($keyword, $province, $city, $companyTypeArr,
                $landType, $areaLower, $areaUpper, $unitPriceLower, $unitPriceUpper,
                $landPriceLower, $landPriceUpper, $isPriceGtAvg, $area, $createdTime, $tags, $companyArea,
                $takeLandDateStart, $takeLandDateEnd,$organizingCode,
                $page, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/purchase/guess-names 02、土地联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataTakenLand
     *
     * @apiDescription 02、土地联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮土地名
     * @apiSuccess {String}  data.list.title       土地名
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            if (empty($keyword)) {
                throw new \Exception('你的关键字没有传');
            }
            $bigDataPurchaseService = new BigDataTakenLandService();
            $rs = $bigDataPurchaseService->guessNames($keyword, $pageSize);
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

}